package com.boxin.ims.modules.wechat.busi;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.framework.base.utils.Constants;
import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.boxin.ims.modules.momarketing.service.QRCodeService;
import com.boxin.ims.modules.recharge.entity.Recharge;
import com.boxin.ims.modules.recharge.service.RechargeService;
import com.boxin.ims.modules.sys.utils.ImageUtils;
import com.boxin.ims.modules.wechat.entity.NewsReply;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.utils.WechatUtils;
import com.thinkgem.jeesite.common.utils.FileUtils;


/**
 * @author Administrator
 * @date 2014-4-1
 * @function <p> 会员中心 </p>
 */
@Service
public class MemberCenterProcessor extends BusinessProcessor {
	@Autowired
	private RechargeService rechargeService;
	
	@Autowired
	private QRCodeService qrCodeService;

	@Override
	public ProcessMessage process() {
		
		WechatMember  member = memberCheckAndReply();
		List<NewsReply> newsReplyList = new ArrayList<NewsReply>();
		if(member != null){
			
			//判断生成   二维码图文
			checkMemberCenterCard(member);
			
			
			//第一项
			NewsReply newsReply = new NewsReply();
			newsReply.setDescription("");
			newsReply.setPicUrl("http://weixin.zhaotang.net/zt/userfiles/memberCard/"   + member.getNumber()+"_nav.png?t="+System.currentTimeMillis());
			newsReply.setUrl("http://weixin.zhaotang.net/zt/f/member/bind?uid="+user.getId()+"&wechatId="+openId);
			
			newsReply.setTitle("会员中心");		//不要标题
			
			newsReplyList.add(newsReply);
			
			//我的物品
			NewsReply myGoodsNewsReply = new NewsReply();
			myGoodsNewsReply.setDescription("");
			myGoodsNewsReply.setPicUrl("");
			myGoodsNewsReply.setUrl("http://weixin.zhaotang.net/zt/f/member/myGoods?uid="+user.getId()+"&wechatId="+openId);
			
			myGoodsNewsReply.setTitle("我的物品");
			newsReplyList.add(myGoodsNewsReply);
			
			//消息记录
			NewsReply myConsumeNewsReply = new NewsReply();
			myConsumeNewsReply.setDescription("");
			myConsumeNewsReply.setPicUrl("");
			myConsumeNewsReply.setUrl("http://weixin.zhaotang.net/zt/f/member/myConsume?uid="+user.getId()+"&wechatId="+openId);
			
			myConsumeNewsReply.setTitle("消费记录");
			newsReplyList.add(myConsumeNewsReply);
			
			//我的推荐
			NewsReply mycommendNewsReply = new NewsReply();
			mycommendNewsReply.setDescription("");
			mycommendNewsReply.setPicUrl("");
			mycommendNewsReply.setUrl("http://weixin.zhaotang.net/zt/f/member/mycommend?uid="+user.getId()+"&wechatId="+openId);
			
			mycommendNewsReply.setTitle("我的推荐");
			newsReplyList.add(mycommendNewsReply);
			
			
			writeMultiImagesToUser(newsReplyList);
			
			
			
		}
		
		
		return null;
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-4-1  下午4:03:45
	 * @function <p> 判断关生成会员卡 导航图文 </p>
	 * @param member
	 */
	public void checkMemberCenterCard(WechatMember member){
		String rootpath = WechatUtils.getMemberCardResourceSavePath(request)+File.separator;
		String  inputFilePath = rootpath +member.getNumber()+".png";
		String outputFilePath = rootpath +member.getNumber()+"_nav.png";

		File inputFile = new File(inputFilePath);
		if(!inputFile.exists()){
			Recharge  recharge = rechargeService.getRechargeByMemberId(member.getId());
			
			if(recharge == null){
				recharge = new Recharge();
				recharge.setMember(member);
				recharge.setMoney(0);
				recharge.setName(member.getName());
				recharge.setNumber(member.getNumber());
				recharge.setType(Recharge.CARD_TYPE_GENERAL);
				recharge.setUser(member.getUser());
				
				rechargeService.save(recharge);
			}
			
			
			
			if(recharge!=null){
				
				if(recharge.getQrCode() == null  || recharge.getQrCode().getId()<=0){	//生成二维码
					QRCode qrcode = new QRCode();
					String content = recharge.getMember().getNumber();
					qrcode.setContent(content);
					String path = Constants.getDefaultQRCodeResourcePath();
					path = WechatUtils.getMemberCardResourceSavePath(request);
					FileUtils.createDirectory(path);
					String filePath = path + File.separator+ content;
					
					QRCodeUtils.generateNoMarginQRCodePNG(content, filePath, 300,300);
					qrcode.setFileName(content+".png");
					qrcode.setPngPath(filePath+".png");
					qrCodeService.save(qrcode);
					
					recharge.setQrCode(qrcode);
					
					rechargeService.save(recharge);
				}
			}
		}
		
		
		File file = new File(outputFilePath);
		if(file.exists()){
			file.delete();
		}
		
		
		try {
			ImageUtils.drawMemberCard(member, inputFilePath, outputFilePath);
		} catch (Exception e) {
			System.out.println("生成会员中心导航二维码失败:");
			e.printStackTrace();
		}
		 
		
	}

	@Override
	public boolean isNeedBeMember() {
		
		return true;
	}
	
	

}
